/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package practice;


import java.io.IOException;
import static chanlib.Chan_Math.*;
import static chanlib.Chan_IO.*;
import static java.lang.Math.min;
import static java.lang.Math.max;

/**
 *
 * @author Chan
 */
public class ThemePark {
    /*
      All neccesary to be defined
      including:
      variable, inner class
    */
    // static int A;
    // static void B();
    // static class C
    /*
       One calculation fits all of the cases.
    */
    static void preReckon(){

    }

    /*
      calculate the answer
    */
    private static void reckon(int line) {
        StringBuilder track=new StringBuilder();
        
        int R=Int();
        int k=Int();
        int N=Int();
        int g[]=new int[N];
        
        for(int i=0;i<N;i++){
            g[i]=Int();
            track.append(g[i]).append(",");
        }
        int visited[]=new int[N];
        for(int i=0;i<N;i++){
            visited[i]=-1;
        }
        int next[]=new int[N];
        long cost[]=new long[N];

        int start=0;
        long totalCost=0;
        for(int r=0;r<R;r++){
            if(visited[start]<0){
                // Processing the first round for the index 'start'.
                visited[start]=r;
                int j=0;
                for(;j<N;j++){
                    if(cost[start]+g[(start+j)%N]>k)break;
                    cost[start]+=g[(start+j)%N];
                }
                totalCost+=cost[start];
                next[start]=(start+j)%N;
                start=(start+j)%N;
            }else{
                int cycle=r-visited[start];
                int cycleR=r+(r-visited[start]);
                long cycleCost=0;
                for(;r<cycleR&&r<R;r++){
                    totalCost+=cost[start];
                    cycleCost+=cost[start];
                    start=next[start];
                }
                totalCost+=(R-r)/cycle*cycleCost;
                r+=(R-r)/cycle*cycle;
                // As it is an advancement of size cycle, 
                // 'start' will come back to the same index
                for(;r<R;r++){
                    totalCost+=cost[start];
                    start=next[start];
                }
            }
        }
        long ans=totalCost;
        String output=String.format("Case #%d: %d",line,ans);
        writeln(output, OUTFILE);
        println(output);
        writeln(String.format("R=%d k=%d N=%d\n%s\n%d", R, k, N,track, ans),ANALFILE);
    }

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) throws IOException {
        /*
           Pre-computing for all cases
        */

        // Just in case!
        preReckon();
        //while((char)read()!='P');

        /*
          Input-output
        */
        boolean test=false;
        boolean small=false;
        String prob="C";
        String filename;
        if(test) filename="test";
        else{
            if(small) filename=prob+"-small-practice";
            else filename=prob+"-large-practice";
        }
        try {
            /*
               Init IO,plus determination of input line number
             */
            int lineNums = initFileIO(filename);
            for (int i = 1; i <= lineNums; i++) {
                reckon(i);
            }
        } finally {
            endIO();
        }
    }
}
